package com.morizhang.usersys.api.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.morizhang.usersys.api.ApiResult;
import com.morizhang.usersys.api.IService;
import com.morizhang.usersys.api.bean.ErrorDefinition;
import com.morizhang.usersys.api.bean.ErrorDefinitionHolder;
import com.morizhang.usersys.api.bean.UserSysException;
import com.morizhang.usersys.api.dao.BasicUserInfoDao;
import com.morizhang.usersys.api.model.UserModel;
import com.morizhang.usersys.api.service.RegistUserService;
import com.morizhang.usersys.api.service.ServiceAnnotation;
import com.morizhang.usersys.api.utils.ErrorUtils;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@ServiceAnnotation(name = "RegistUser")
public class RegistUserServiceImpl implements RegistUserService, IService {
    @Autowired
    protected BasicUserInfoDao basicUserInfoDao;

    protected UserSysException userSysException;

    @Override
    @Transactional
    public String registNewUser(String userName, String password) throws Exception {
        UserModel newUser = new UserModel();
        newUser.setUserName(userName);
        newUser.setPassword(password);
        basicUserInfoDao.addNewUser(newUser);
        newUser = basicUserInfoDao.selectUserByUserName(userName);
        return newUser.getUserID();
    }

    @Override
    public ApiResult callService(JSONObject requestParams) throws Exception {
        ApiResult result = new ApiResult();
        JSONObject requestParamsData = requestParams.getJSONObject("data");
        try {
            String userName = requestParamsData.getString("userName");
            if (userName == null || userName.length() == 0) {
                ErrorDefinition error = ErrorDefinitionHolder.get("NO_REGIST_USER_NAME");
                userSysException = ErrorUtils.convertErrorToException(error);
                throw userSysException;
            }
            String password = requestParamsData.getString("password");
            if (password == null || password.length() == 0) {
                ErrorDefinition error = ErrorDefinitionHolder.get("NO_REGIST_PASSWORD");
                userSysException = ErrorUtils.convertErrorToException(error);
                throw userSysException;
            }
            String newUserID = registNewUser(userName, password);
            result.setSuccess(true);
            result.setData(newUserID);
            return result;
        } catch (UserSysException e) {
            result.setSuccess(false);
            result.setFaultMessage(e.getExceptionMessage());
            result.setException(e);
            return result;
        } catch (MyBatisSystemException e) {
            ErrorDefinition error = ErrorDefinitionHolder.get("REGIST_WRITE_ERROR");
            UserSysException ue = ErrorUtils.convertErrorToException(error);
            result.setSuccess(false);
            result.setFaultMessage(ue.getExceptionMessage());
            result.setException(ue);
            return result;
        }
    }
}
